package com.hgh.model;

import com.hgh.util.GenerateEnemyTank;
import com.misakubex.BTFrame;
import com.misakubex.Main;
import com.misakubex.TankLocal;


public class Missile implements Runnable {

    public static int size=10;

    //x坐标
    private int x;

    //y坐标
    private int y;

    //运动方向
    private char direction;

    //运动速度
    public static final int SPEED = 100;

    /**
     * 所属坦克
     */
    private Tank tank;

    /**
     * 是否敌机发射出来的，是的话true
     */
    private boolean fromEnemy;

    public Missile(Tank tank){
        this.tank = tank;
        this.x = tank.getX();
        this.y = tank.getY();
        this.direction = tank.getDirct();
        this.fromEnemy = tank.isEnemy();
    }

    @Override
    public void run() {
        while(true){
            try {
                Thread.sleep(SPEED);
                //让子弹休息，以此来作为速度标记
            } catch (Exception e) {
                e.printStackTrace();
            }
            //如果当前子弹不在子弹列表中，说明子弹已经死去
            if(!BTFrame.missileList.contains(this)){
                Tank.addMissileNum(tank);
                break;
            }
            switch(direction){
                case 'w':
                    //向上
                    y-= Main.a;
                    break;
                case 's':
                    //向下
                    y+=Main.a;
                    break;
                case 'a':
                    // /向 左
                    x-=Main.a;
                    break;
                case 'd':
                    //向右
                    x+=Main.a;
                    break;
                default:

            }
            //子弹何时死亡
            //判断该子弹是否碰到地图的边缘
            if(x<0||x>25*Main.a||y<0||y>25*Main.a){
                Tank.addMissileNum(tank);
                BTFrame.removeMissile(this);
                break;
            }
            if(Map.points[x/Main.a][y/Main.a]==Map.REDWALL){
                Map.points[x/Main.a][y/Main.a]='n';
                Tank.addMissileNum(tank);
                BTFrame.removeMissile(this);
                break;
            }
            if(Map.points[x/Main.a][y/Main.a]==Map.WHITEWALL){
                Tank.addMissileNum(tank);
                BTFrame.removeMissile(this);
                break;
            }
            if(x/Main.a==Map.base[0]&&y/Main.a==Map.base[1]){
                BTFrame.isFail = true;
                break;
            }
            if(destroyMissileWhenTwoMissileMeet()||destroyMissileWhenMeetEnemyTank()){
                break;
            }
        }

    }



    /**
     * 判断是否与敌机相撞
     */
    public boolean destroyMissileWhenMeetEnemyTank(){
        if(!fromEnemy){
            for(int index=1;index<=3;index++){
                for(EnemyTank enemyTank : GenerateEnemyTank.getEnemyTankList(index)){
                    if(enemyTank.getX()==x&&enemyTank.getY()==y){
                        TankLocal.killTankNum+=1;
                        if(TankLocal.killTankNum>=20){
                            BTFrame.isWin=true;
                        }
                        BTFrame.removeTank(index,enemyTank);
                        BTFrame.removeMissile(this);
                        Tank.addMissileNum(tank);
                        return true;
                    }
                }
            }
        }
        else{
            if(BTFrame.tLocal.ta.getX()==x&&BTFrame.tLocal.ta.getY()==y){
                TankLocal.bleed-=1;
                if(TankLocal.bleed<=0){
                    BTFrame.isFail=true;
                }
                BTFrame.tLocal.ta.setX(10*Main.a);
                BTFrame.tLocal.ta.setY(25*Main.a);
                BTFrame.removeMissile(this);
                Tank.addMissileNum(tank);
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否与敌方子弹相撞，相撞则抵消
     */
    public boolean destroyMissileWhenTwoMissileMeet(){
       for(Missile missile : BTFrame.missileList){
           if(missile.getX()==x&&missile.getY()==y&&missile.isFromEnemy()!=fromEnemy){
               BTFrame.removeMissile(missile);
               BTFrame.removeMissile(this);
               Tank.addMissileNum(tank);
               return true;
           }
       }
       return false;
    }

    public char getDirection() {
        return direction;
    }

    public void setDirection(char direction) {
        this.direction = direction;
    }

    public boolean isFromEnemy() {
        return fromEnemy;
    }

    public void setFromEnemy(boolean fromEnemy) {
        this.fromEnemy = fromEnemy;
    }


    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }
}
